<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Valery Stukanov
 * Date: 7/1/11
 * Time: 10:56 AM
 */


class ZendX_Sphinx_Result implements Iterator
{
    protected $_data = array();
    protected $_total = 0;
    protected $_cache = false;
    protected $_sign = NULL;
    protected $_easy = false;

    public function useCache($use = false, $sign, $easyWay = false) {
        $this->_cache = $use;
        $this->_sign = $sign;
        $this->_easy = $easyWay;
    }

    public function current()
    {
        current($this->_data);
    }

    public function next()
    {
        next($this->_data);
    }

    public function key()
    {
        return key($this->_data);
    }

    public function valid()
    {
        return NULL !== $this->key();
    }

    public function rewind()
    {
        reset($this->_data);
    }

    public function __construct(array $data, $total = NULL)
    {
        $this->_data = $data;
        $this->_total = $total;
    }

    public function collect($group = NULL, $value = NULL)
    {
        return apiDB::CollectItems($this->_data, $group, $value);
    }

    public function getIds($page = 1, $ipp = 10)
    {
        if(!$this->_easy) {
            $cache = Zend_Registry::get('Zend_Cache');
            if(FALSE === ($result = $cache->load("applicationSphinxId$page$ipp{$this->_sign}")) || !$this->_cache) {
                $result = array();
                foreach ($this->_data as $item) {
                    $result[] = $item['id'];
                }
                $result = array_slice($result, ($page - 1) *  $ipp, $ipp);
                if($this->_cache) {
                    $cache->save($result, "applicationSphinxId$page$ipp{$this->_sign}");
                }
            }
        } else {
            $result = array();
            foreach ($this->_data as $item) {
                $result[] = $item['id'];
            }
        }
        return $result;
    }

    /**
     * @param string $type
     * @param bool $is_unique
     * @return array
     */
    public function getElements($type, $is_unique = false) {
        $cache = Zend_Registry::get('Zend_Cache');
        $up = $is_unique ? 'unique' : 'nounique';
        if(!$this->_easy) {
            $cache = Zend_Registry::get('Zend_Cache');
            if(FALSE === ($result = $cache->load("applicationSphinx$type$up{$this->_sign}")) || !$this->_cache) {
                $result = array();
                foreach ($this->_data as $item) {
                    $result[] = $item[$type];
                }
                if($is_unique){
                    $result = array_unique($result);
                }
                if($this->_cache) {
                    $cache->save($result, "applicationSphinx$type$up{$this->_sign}");
                }
            }
        } else {
            if(FALSE === ($result = $cache->load("applicationSphinxAll$type$up"))) {
                $select = NULL;
                switch($type) {
                    case 'top_id': {
                        $table = new ZendX_Db_Table('categories');
                        $select = $table->select()->where('parent_id = 0');
                        break;
                    }
                    case 'category_id': {
                        $table = new ZendX_Db_Table('categories');
                        $select = $table->select()->where('parent_id != 0');
                        break;
                    }
                    case 'brand_id': {
                        $table = new ZendX_Db_Table('brands');
                        break;
                    }
                }

                foreach($table->fetchAll($select) as $item) {
                    $result[] = $item->id;
                }
                $cache->save($result, "applicationSphinxAll$type$up");
            }
        }
        return $result;
    }

    public function getTotal()
    {
        return $this->_total;
    }

}
